package com.anti.modular.sys.emp.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Dict;
import com.anti.jdbc.DBUtils;
import com.anti.modular.sys.emp.entity.SysEmpExtOrgPos;
import com.anti.modular.sys.emp.service.SysEmpExtOrgPosService;
import com.anti.modular.sys.org.entity.SysOrg;
import com.anti.modular.sys.org.service.SysOrgService;
import com.anti.modular.sys.pos.entity.SysPos;
import com.anti.modular.sys.pos.service.SysPosService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2021-3-11
 */
@Service
public class SysEmpExtOrgPosServiceImpl implements SysEmpExtOrgPosService {
    @Resource
    DBUtils dbUtils;

    /**
     * 附属机构id参数键
     */
    private static final String EXT_ORG_ID_DICT_KEY = "orgId";

    /**
     * 附属机构编码参数键
     */
    private static final String EXT_ORG_CODE_DICT_KEY = "orgCode";

    /**
     * 附属机构名称参数键
     */
    private static final String EXT_ORG_NAME_DICT_KEY = "orgName";

    /**
     * 附属职位id参数键
     */
    private static final String EXT_POS_ID_DICT_KEY = "posId";

    /**
     * 附属职位编码参数键
     */
    private static final String EXT_POS_CODE_DICT_KEY = "posCode";

    /**
     * 附属职位名称参数键
     */
    private static final String EXT_POS_NAME_DICT_KEY = "posName";
    @Override
    public boolean hasExtPosEmp(Long posId) {
        String sql="select count(*) from sys_emp_ext_org_pos where pos_id=?";
        Integer integer = dbUtils.getFirst(sql, Integer.class, posId);
        return integer >= 1;
    }

    @Override
    public boolean hasExtOrgEmp(Long orgId) {
        String sql="select count(*) from sys_emp_ext_org_pos where org_id=?";
        Integer integer = dbUtils.getFirst(sql, Integer.class, orgId);
        return integer >= 1;
    }

    @Override
    public void addOrEdit(Long empId, List<Dict> extIdList) {
        //删除附属信息
        String removeSql="delete from sys_emp_ext_org_pos where emp_id =?";
        dbUtils.execSql(removeSql,empId);
        //保存附属信息
        String insertSql="insert into sys_emp_ext_org_pos (emp_id,org_id,pos_id) values (?,?,?)";
        extIdList.forEach(dict -> {
            Long orgId = dict.getLong(EXT_ORG_ID_DICT_KEY);
            Long posId = dict.getLong(EXT_POS_ID_DICT_KEY);
            dbUtils.execSql(insertSql,empId,orgId,posId);
        });
    }

    @Override
    public List<Dict> getEmpExtOrgPosDictList(Long empId, boolean isFillId) {
        List<Dict> dictList = CollectionUtil.newArrayList();
        String sql="select * from sys_emp_ext_org_pos where emp_id=?";
        List<SysEmpExtOrgPos> sysEmpExtOrgPosList = dbUtils.query(sql, SysEmpExtOrgPos.class, empId);
        sysEmpExtOrgPosList.forEach(sysEmpExtOrgPos -> {
            Dict dict = Dict.create();
            Long orgId = sysEmpExtOrgPos.getOrgId();
            SysOrg sysOrg = dbUtils.getFirst("SELECT * FROM SYS_ORG WHERE ID=?", SysOrg.class, orgId);;
            dict.put(EXT_ORG_CODE_DICT_KEY, sysOrg.getCode());
            dict.put(EXT_ORG_NAME_DICT_KEY, sysOrg.getName());

            Long posId = sysEmpExtOrgPos.getPosId();
            SysPos sysPos = dbUtils.getFirst("SELECT * FROM SYS_POS WHERE ID=?", SysPos.class, posId);;

            dict.put(EXT_POS_CODE_DICT_KEY, sysPos.getCode());
            dict.put(EXT_POS_NAME_DICT_KEY, sysPos.getName());
            if (isFillId) {
                dict.put(EXT_ORG_ID_DICT_KEY, orgId);
                dict.put(EXT_POS_ID_DICT_KEY, posId);
            }
            dictList.add(dict);
        });

        return dictList;
    }

    @Override
    public void deleteEmpExtInfoByUserId(Long empId) {
        String sql="delete from sys_emp_ext_org_pos where emp_id=?";
        dbUtils.execSql(sql,empId);
    }
}
